package com.yxx.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckRole;
import com.yxx.annotation.OptLogger;
import com.yxx.entityModel.vo.Result.PageResult;
import com.yxx.entityModel.vo.Result.ResponseResult;
import com.yxx.entityModel.vo.query.OnlineUserQuery;
import com.yxx.entityModel.vo.query.UserQuery;
import com.yxx.entityModel.vo.request.DisableRequest;
import com.yxx.entityModel.vo.request.PasswordRequest;
import com.yxx.entityModel.vo.request.UserRoleRequest;
import com.yxx.entityModel.vo.response.*;
import com.yxx.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.metadata.ValidateUnwrappedValue;
import java.util.List;

import static com.baomidou.mybatisplus.core.assist.ISqlRunner.UPDATE;
import static com.yxx.constant.OptTypeConstant.KICK;

/**
 * @author yuxiangxun
 * @date 2025/1/21
 * @apiNote
 */
@Api(tags = "用户模块")
@Slf4j
@RestController
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 重点
     * 获取后台登录用户信息
     * @return
     */
    @GetMapping("/admin/user/getUserInfo")
    @ApiOperation(value = "获取后台登录用户信息")
    public ResponseResult<UserBackInfoResponse> getUserBackInfo(){
        return ResponseResult.success(userService.getUserBackInfo());
    }

    /**
     * 重点
     * 获取登录用户菜单
     * 管理员和用户访问后台有不同的权限，应该有不一样的路由菜单显示
     * @return {@link RouterResponse} 登录用户菜单
     */
    @GetMapping("/admin/user/getUserMenu")
    @ApiOperation(value = "获取登录用户菜单")
    public ResponseResult<List<RouterResponse>> getUserMenu(){
        return ResponseResult.success(userService.getUserMenu());
    }

    /**
     * 查看后台用户列表（分页查询）
     * @param userQuery 用户查询条件
     * @return 用户后台列表
     */
    @ApiOperation(value = "查看后台用户列表（分页查询）")
    @SaCheckPermission("system:user:list")
    @GetMapping("/admin/user/list")
    public ResponseResult<PageResult<UserBackResponse>> userBackListVOList(UserQuery userQuery){
        return ResponseResult.success(userService.userBackListVOList(userQuery));
    }

    /**
     * 查看用户角色选项
     * @return
     */
    @ApiOperation(value = "查看用户角色选项")
    @SaCheckPermission("system:user:list")
    @GetMapping("/admin/user/role")
    public ResponseResult<List<UserRoleResponse>> userRoleDTOList(){
        return ResponseResult.success(userService.userRoleDTOList());
    }

    /**
     * 修改用户信息
     * @param userRoleRequest
     * @return
     */
    @OptLogger(value = UPDATE)
    @PutMapping("/admin/user/update")
    @SaCheckPermission("system:user:update")
    @ApiOperation("修改用户信息")
    public ResponseResult<?> updateUser(@Validated @RequestBody UserRoleRequest userRoleRequest){
        userService.updateUser(userRoleRequest);
        return ResponseResult.success();
    }

    /**
     * 修改用户状态
     * @param disableRequest 禁用请求信息
     * @return
     */
    @OptLogger(value = UPDATE)
    @ApiOperation(value = "修改用户状态")
    @SaCheckPermission("system:user:status")
    @PutMapping("/admin/user/changeStatus")
    public ResponseResult<?> updateUserStatus(@Validated @RequestBody DisableRequest disableRequest){
        userService.updateUserStatus(disableRequest);
        return ResponseResult.success();
    }

    /**
     * 查看在线用户列表（分页查询）
     * @param onlineUserQuery 查询条件
     * @return 在线用户列表
     */
    @ApiOperation(value = "查看在线用户")
    @SaCheckPermission("monitor:online:list")
    @GetMapping("/admin/online/list")
    public ResponseResult<PageResult<OnlineUserResponse>> listOnlineUser(OnlineUserQuery onlineUserQuery){
        return ResponseResult.success(userService.onlineUserList(onlineUserQuery));
    }

    /**
     * 下线用户
     *
     * @param token 在线token
     * @return {@link ResponseResult<>}
     */
    @OptLogger(value = KICK)
    @ApiOperation(value = "下线用户")
    @SaCheckPermission("monitor:online:kick")
    @GetMapping("/admin/online/kick/{token}")
    public ResponseResult<?> kickOutUser(@PathVariable("token") String token) {
        userService.kickOutUser(token);
        return ResponseResult.success();
    }



    /**
     * 修改管理员密码
     * @param passwordRequest
     * @return
     */
    @GetMapping("/admin/password")
    @ApiOperation(value = "修改管理员密码")
    @SaCheckRole("1")
    public ResponseResult<?> updateAdminPassword(@RequestBody @Validated PasswordRequest passwordRequest){
        userService.updateAdminPassword(passwordRequest);
        return ResponseResult.success();
    }

}
